<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务列表</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f4f8;
            display: flex;
            justify-content: center;
            padding: 50px;
        }
        .task-container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            width: 400px;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .input-group {
            display: flex;
            margin-bottom: 20px;
        }
        .input-group input {
            flex: 1;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 5px 0 0 5px;
            outline: none;
        }
        .input-group button {
            padding: 10px 20px;
            font-size: 16px;
            border: none;
            background-color: #4caf50;
            color: white;
            cursor: pointer;
            border-radius: 0 5px 5px 0;
        }
        .input-group button:hover {
            background-color: #45a049;
        }
        ul {
            list-style-type: none;
            padding: 0;
        }
        li {
            display: flex;
            align-items: center;
            padding: 10px;
            border-bottom: 1px solid #eee;
        }
        li:last-child {
            border-bottom: none;
        }
        .task-text {
            flex: 1;
            font-size: 16px;
            color: #555;
        }
        .delete-btn {
            background-color: #f44336;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 3px;
            cursor: pointer;
        }
        .delete-btn:hover {
            background-color: #d32f2f;
        }
        .no-tasks {
            text-align: center;
            color: #999;
            font-style: italic;
        }
    </style>
</head>
<body>

    <div class="task-container">
        <h1>任务列表</h1>
        <div class="input-group">
            <input type="text" id="taskInput" placeholder="输入新任务">
            <button id="addTaskBtn">添加</button>
        </div>
        <ul id="taskList">
            <!-- 任务项将在这里添加 -->
        </ul>
    </div>

    <script>
        // 获取元素
        const taskInput = document.getElementById('taskInput');
        const addTaskBtn = document.getElementById('addTaskBtn');
        const taskList = document.getElementById('taskList');

        // 添加任务函数
        function addTask() {
            const taskText = taskInput.value.trim();

            if (taskText === '') {
                alert('请输入任务内容！');
                return;
            }

            // 创建列表项
            const li = document.createElement('li');

            // 创建任务文本
            const taskSpan = document.createElement('span');
            taskSpan.className = 'task-text';
            taskSpan.textContent = taskText;

            // 创建删除按钮
            const deleteBtn = document.createElement('button');
            deleteBtn.className = 'delete-btn';
            deleteBtn.textContent = '删除';
            deleteBtn.onclick = function() {
                taskList.removeChild(li);
                checkNoTasks();
            };

            // 组装列表项
            li.appendChild(taskSpan);
            li.appendChild(deleteBtn);
            taskList.appendChild(li);

            // 清空输入框并聚焦
            taskInput.value = '';
            taskInput.focus();

            // 检查是否有任务
            checkNoTasks();
        }

        // 检查是否有任务并显示提示
        function checkNoTasks() {
            const items = taskList.getElementsByTagName('li');
            if (items.length === 0) {
                const noTasks = document.createElement('div');
                noTasks.className = 'no-tasks';
                noTasks.textContent = '暂无任务。';
                taskList.appendChild(noTasks);
            } else {
                const noTasks = taskList.getElementsByClassName('no-tasks')[0];
                if (noTasks) {
                    taskList.removeChild(noTasks);
                }
            }
        }

        // 事件监听器
        addTaskBtn.addEventListener('click', addTask);

        // 回车键提交
        taskInput.addEventListener('keyup', function(event) {
            if (event.key === 'Enter') {
                addTask();
            }
        });
    </script>

</body>
</html>